

import java.util.ArrayList;

public class Player extends Entity 
{
	Speed speed;
	boolean isTouchingGround = false;

	long lastUpdate = System.currentTimeMillis();
	boolean facingLeft = false;
	boolean stand = false;
	
	int health = 300;
	
	public Player(Game game, int x, int y, SpriteImage sprite)
	{
		super(game, x, y, sprite);
		speed = new Speed(x, y);
	}
	@Override
	public void update(long gameTime) 
	{
		System.out.println(x);
		if (speed.getvX() == 0)
		{
			if (!facingLeft)
			{
				sprite = game.getStorage().getSprite("res//stand.png", 1);
				sprite.pause = true;
				stand = true;
			}
			else
			{
				sprite = game.getStorage().getSprite("res//standLeft.png", 1);
				sprite.pause = true;
				stand = true;
			}
		}
		else
		{
			stand = false;
			sprite.pause = false;
		}
		if (speed.getvX() > 0)
		{
			sprite = game.getStorage().getSprite("res//Walk_Right.png", 5	);
			facingLeft = false;
		}
		if (speed.getvX() < 0)
		{
			sprite = game.getStorage().getSprite("res//Walk_Left.png", 5);
			facingLeft = true;
		}
		
		if (collidesWithWater())
		{
			x =  x + (int)((gameTime - lastUpdate) / 10 * speed.getvX() * .5);
		} else
		{
			x =  x + (int)((gameTime - lastUpdate) / 10 * speed.getvX());
		}

		collidesHorizontal();

		if (collidesWithWater())
		{
			y =  y + (int)((gameTime - lastUpdate) / 10 * speed.getvY());
		} else
		{
			y =  y + (int)((gameTime - lastUpdate) / 10 * speed.getvY());
		}
		collidesVertical();
		if (!isTouchingGround && collidesWithWater())
		{
			speed.waterGravity();
		}
		
		if (!isTouchingGround && !collidesWithWater())
		{
			speed.gravity();
		}
		lastUpdate = gameTime;

		collidesWithWater();
	}

	public boolean collidesVertical()
	{
		ArrayList<Entity> adjacentObjects = findAdjacentTiles();
		for (Entity objects : adjacentObjects)
		{
			isTouchingGround = false;
			if (objects instanceof Collidable)
			{
				if (collidesTop(objects))
				{
					y = objects.y + objects.sprite.height;
					speed.setvY(0);
				}
				if (collidesBottom(objects))
				{
					y = objects.y - sprite.height; 
					speed.setvY(0);
					isTouchingGround = true;
				}
				else
				{
					isTouchingGround = false;
				}

			}
		}
		if (adjacentObjects.size() == game.objects.size())
		{
			isTouchingGround = false;
		}
		
		for (Entity objects: game.objects)
		{
			if (objects instanceof Collidable)
			{
				if (collidesBottom(objects))
				{
					y = objects.y - sprite.height; 
					speed.setvY(0);
					isTouchingGround = true;
				}
				else
				{
					isTouchingGround = false;
				}
			}
		}
		return false;
	}
	
	public boolean collidesHorizontal()
	{
		ArrayList<Entity> adjacentTiles = findAdjacentTiles();
		for (Entity tile : adjacentTiles)
		{
			if (tile instanceof Collidable)
			{
				if (collidesLeft(tile))
				{
					x = tile.x + tile.sprite.frameWidth; 
				}
				if (collidesRight(tile))
				{
					x = tile.x - sprite.frameWidth;
				}
			}
		}
		return false;
	}
	
	public boolean collidesWithWater()
	{
		ArrayList<Entity> adjacentTiles = findAdjacentTiles();
		for (Entity tile : adjacentTiles)
		{
			if (tile.getClass().equals(WaterTile.class))
			{
				if (collidesWith(tile))
				{
					return true;
				}
			}
		}
		return false;
	}
	
	public void jump()
	{
		/*
		 * if onGround
		 * speed.setvY(10)  
		 */
		ArrayList<Entity> adjacentTiles = findAdjacentTiles();
		for (Entity tile : adjacentTiles)
		{
			if (tile instanceof Collidable)
			{
				
				if (collidesBottom(tile) && !collidesWithWater())
				{
					game.setVelocityY(-8);
				}
				if (collidesBottom(tile) && collidesWithWater())
				{
					game.setVelocityY(-6);
				}
			}
		}
	}

}
